/**
 * 用于分页列表配置项的辅助初始化
 */
import { h } from 'vue'
import { NButton, NFlex } from 'naive-ui'
import { ComponentEnum } from '@/enum/ComponentEnum.ts'
import { ArrayUtil } from '@/util/ArrayUtil.ts'
import { PeaceColumn } from '@/components/type/PeaceColumn'
import { PeaceActionBtn } from '@/components/type/PeaceActionBtn'
import { ObjectUtil } from '@/util/ObjectUtil'
import { BooleanUtil } from '@/infrastructure/PeaceDataTable/util/BooleanUtil'
import { initColumnPeaceOption } from '@/infrastructure/PeaceDataTable/util/OptionUtil.ts'
import PeaceRepeatRender from '@/components/type/PeaceRepeatRender'
import { markFun } from '@/infrastructure/PeaceDataTable/util/PeaceColumnUtil'
import { DateTypeEnum } from '@/enum/DateTypeEnum'

/**
 * 初始化 peaceOption,filterConfig,detailConfig,saveConfig
 */
export const initPeaceColumn = (columnList: PeaceColumn[],tableAction:(rowData?: object, rowIndex?: number, item: PeaceActionBtn) => void ) => {
  if (ArrayUtil.isEmpty(columnList)) {
    return columnList
  }
  for (const columnEle of columnList) {
    if (columnEle.hasOwnProperty('type')) {
      continue
    }
    initColumnPeaceMarkFun(columnEle)
    // initColumnOption(columnEle)
    initColumnPeaceOption(columnEle.optionConfig)
    initColumnFilterConfig(columnEle)
    initColumnPeaceOperateBtn(columnEle,tableAction)
    initColumnDetailConfig(columnEle)
    initColumnSaveConfig(columnEle)
    initColumnAddConfig(columnEle)
    initColumnUpdateConfig(columnEle)
    initColumnCopyConfig(columnEle)
    initColumnRepeatRender(columnEle)
  }
  return columnList
}

const initColumnPeaceMarkFun = (ele:PeaceColumn) => {
  ele.markFun = markFun
}
const initColumnRepeatRender = (ele:PeaceColumn) => {
  if (ele.filterConfig?.repeatRender){
    ele.filterConfig.repeatRender = new PeaceRepeatRender(
      ele.filterConfig.repeatRender.format
      ,ele.filterConfig.repeatRender.split
      ,ele.filterConfig.repeatRender.num
      ,ele.filterConfig.repeatRender.fieldNames
    )
    ele.filterConfig.repeatRender.initMarkFileNames(ele.filterConfig.key)
  }
  if (ele.saveConfig?.repeatEditRender){
    ele.saveConfig.repeatEditRender = new PeaceRepeatRender(
      ele.saveConfig.repeatEditRender.format
      ,ele.saveConfig.repeatEditRender.split
      ,ele.saveConfig.repeatEditRender.num
      ,ele.saveConfig.repeatEditRender.fieldNames
    )
    ele.saveConfig.repeatEditRender.initMarkFileNames(ele.filterConfig.key)
  }
  if (ele.saveConfig?.repeatAddRender){
    ele.saveConfig.repeatAddRender = new PeaceRepeatRender(
      ele.saveConfig.repeatAddRender.format
      ,ele.saveConfig.repeatAddRender.split
      ,ele.saveConfig.repeatAddRender.num
      ,ele.saveConfig.repeatAddRender.fieldNames
    )
    ele.saveConfig.repeatAddRender.initMarkFileNames(ele.filterConfig.key)
  }
  if (ele.saveConfig?.repeatUpdateRender){
    ele.saveConfig.repeatUpdateRender = new PeaceRepeatRender(
      ele.saveConfig.repeatUpdateRender.format
      ,ele.saveConfig.repeatUpdateRender.split
      ,ele.saveConfig.repeatUpdateRender.num
      ,ele.saveConfig.repeatUpdateRender.fieldNames
    )
    ele.saveConfig.repeatUpdateRender.initMarkFileNames(ele.filterConfig.key)
  }
  if (ele.saveConfig?.repeatCopyRender){
    ele.saveConfig.repeatCopyRender = new PeaceRepeatRender(
      ele.saveConfig.repeatCopyRender.format
      ,ele.saveConfig.repeatCopyRender.split
      ,ele.saveConfig.repeatCopyRender.num
      ,ele.saveConfig.repeatCopyRender.fieldNames
    )
    ele.saveConfig.repeatCopyRender.initMarkFileNames(ele.filterConfig.key)
  }
  if (ele.addConfig?.repeatRender){
    ele.addConfig.repeatRender = new PeaceRepeatRender(
      ele.addConfig.repeatRender.format
      ,ele.addConfig.repeatRender.split
      ,ele.addConfig.repeatRender.num
      ,ele.addConfig.repeatRender.fieldNames
    )
    ele.addConfig.repeatRender.initMarkFileNames(ele.filterConfig.key)
  }
  if (ele.updateConfig?.repeatRender){
    ele.updateConfig.repeatRender = new PeaceRepeatRender(
      ele.updateConfig.repeatRender.format
      ,ele.updateConfig.repeatRender.split
      ,ele.updateConfig.repeatRender.num
      ,ele.updateConfig.repeatRender.fieldNames
    )
    ele.updateConfig.repeatRender.initMarkFileNames(ele.filterConfig.key)
  }
  if (ele.copyConfig?.repeatRender){
    ele.copyConfig.repeatRender = new PeaceRepeatRender(
      ele.copyConfig.repeatRender.format
      ,ele.copyConfig.repeatRender.split
      ,ele.copyConfig.repeatRender.num
      ,ele.copyConfig.repeatRender.fieldNames
    )
    ele.copyConfig.repeatRender.initMarkFileNames(ele.filterConfig.key)
  }
}
/**
 * 初始化PeaceColumn的able类字段;
 * 初始化配置项的able字段,autoExtend字段,key字段,optionConfig字段,componentEnum字段
 * @param ele 列配置项
 * @param configField 指定 PeaceColumn 的哪个配置项名,例如 PeaceColumn 的 filterConfig
 * @param ableFieldName 指定 配置项 的 able类字段,例如 filterConfig 的 peaceFilterable
 */
const initAbleConfigAndSoOn = (ele: PeaceColumn,configFieldName:string,ableFieldName:string) => {
  ele[ableFieldName] = ObjectUtil.isNullOrUndefined(ele[ableFieldName])?true:ele[ableFieldName]
  // 初始化 filterConfig
  const ifEmptyFilterConfig = ObjectUtil.isEmpty(ele[configFieldName])
  if (ifEmptyFilterConfig) {
    ele[configFieldName] = {}
  }
  if (!ifEmptyFilterConfig) {
    if (ObjectUtil.isNullOrUndefined(ele[configFieldName].autoExtend)){
      ele[configFieldName].autoExtend = false
    }
  } else {
    ele[configFieldName].autoExtend = true
  }
  if (ObjectUtil.isNullOrUndefined(ele[configFieldName][ableFieldName]) && ele[configFieldName].autoExtend) {
    ele[configFieldName][ableFieldName] = ele[ableFieldName]
  }
  if (ObjectUtil.isNullOrUndefined(ele[configFieldName].key) && ele[configFieldName].autoExtend) {
    ele[configFieldName].key = ele?.key
  }
  if (ObjectUtil.isNullOrUndefined(ele[configFieldName].title) && ele[configFieldName].autoExtend) {
    ele[configFieldName].title = ele?.title
  }
  if (ObjectUtil.isNotNullAndUndefined(ele[configFieldName].optionConfig)){
    ele[configFieldName].optionConfig = initColumnPeaceOption(ele[configFieldName].optionConfig)
  }else if (ObjectUtil.isNullOrUndefined(ele[configFieldName].optionConfig) && ele[configFieldName].autoExtend) {
    ele[configFieldName].optionConfig = ele?.optionConfig
  }
  if (ObjectUtil.isNullOrUndefined(ele[configFieldName].componentEnum) && ele[configFieldName].autoExtend) {
    ele[configFieldName].componentEnum = ele?.componentEnum
  }
}

const initColumnCopyConfig = (ele: PeaceColumn) => {
  if (ele.copyConfig){
    return
  }
  if (ObjectUtil.isNullOrUndefined(ele.saveConfig)) {
    if (ele.peaceCopyable || ele.peaceEditable) {
      initAbleConfigAndSoOn(ele, "copyConfig", "show")
      ele.copyConfig.show = ele.saveConfig.peaceCopyable || ele.peaceEditable
    }
    return
  }
  ele.copyConfig = {}
  Object.assign(ele.copyConfig, ele.saveConfig)
  ele.copyConfig.disable = false
  ele.copyConfig.show = ele.saveConfig.peaceUpdatable || ele.saveConfig.peaceEditable
  ele.copyConfig.peaceRender = ele.saveConfig.peaceCopyRender ?? ele.saveConfig.peaceEditRender
  ele.copyConfig.repeatRender = ele.saveConfig.repeatCopyRender ?? ele.saveConfig.repeatEditRender
}
const initColumnUpdateConfig = (ele: PeaceColumn) => {
  if (ele.updateConfig){
    return
  }
  if (ObjectUtil.isNullOrUndefined(ele.saveConfig)) {
    if (ele.peaceUpdatable || ele.peaceEditable) {
      initAbleConfigAndSoOn(ele, "updateConfig", "show")
      ele.copyConfig.show = ele.saveConfig.peaceUpdatable || ele.peaceEditable
    }
    return
  }
  ele.updateConfig = {}
  Object.assign(ele.updateConfig, ele.saveConfig)
  ele.updateConfig.disable = false
  ele.updateConfig.show = ele.saveConfig.peaceUpdatable || ele.saveConfig.peaceEditable
  ele.updateConfig.peaceRender = ele.saveConfig.peaceUpdateRender ?? ele.saveConfig.peaceEditRender
  ele.updateConfig.repeatRender = ele.saveConfig.repeatUpdateRender ?? ele.saveConfig.repeatEditRender
}
const initColumnAddConfig = (ele: PeaceColumn) => {
  if (ele.addConfig){
    return
  }
  if (ObjectUtil.isNullOrUndefined(ele.saveConfig)) {
    if (ele.peaceAddable || ele.peaceEditable) {
      initAbleConfigAndSoOn(ele,"addConfig","show")
      ele.copyConfig.show =  ele.saveConfig.peaceAddable || ele.peaceEditable
    }
    return
  }
  ele.addConfig = {}
  Object.assign(ele.addConfig, ele.saveConfig)
  ele.addConfig.disable = false
  ele.addConfig.show = ele.saveConfig.peaceAddable || ele.saveConfig.peaceEditable
  ele.addConfig.peaceRender = ele.saveConfig.peaceAddRender ?? ele.saveConfig.peaceEditRender
  ele.addConfig.repeatRender = ele.saveConfig.repeatAddRender ?? ele.saveConfig.repeatEditRender
  if (ele.addConfig.repeatRender){
    ele.addConfig.repeatRender.initMarkFileNames(ele.filterConfig.key)
  }
}
/**
 * 初始化列保存配置
 * @param ele 列配置项
 */
const initColumnSaveConfig = (ele: PeaceColumn) => {
  initAbleConfigAndSoOn(ele,'saveConfig','peaceEditable')
  initAbleConfigAndSoOn(ele,'saveConfig','peaceAddable')
  initAbleConfigAndSoOn(ele,'saveConfig','peaceUpdatable')
  initAbleConfigAndSoOn(ele,'saveConfig','peaceCopyable')
  if (ele.saveConfig && ObjectUtil.isNullOrUndefined(ele.saveConfig.dateType)) {
    ele.saveConfig.dateType = DateTypeEnum.DATE
  }
  if (ele.saveConfig.repeatRender){
    ele.saveConfig.repeatRender.initMarkFileNames(ele.saveConfig.key)
  }
  /*// 初始化保存配置
  if (!ele.saveConfig) {
    ele.saveConfig = {}
  }

  // 设置默认的编辑状态
  if (ele.peaceEditable === undefined) {
    ele.peaceEditable = true
  }
  if (ele.saveConfig.peaceEditable === undefined) {
    ele.saveConfig.peaceEditable = ele.peaceEditable
  }

  // 设置默认的新增状态
  if (ele.peaceAddable === undefined) {
    ele.peaceAddable = true
  }
  if (ele.saveConfig.peaceAddable === undefined) {
    ele.saveConfig.peaceAddable = ele.peaceAddable
  }

  // 设置默认的复制状态
  if (ele.peaceCopyable === undefined) {
    ele.peaceCopyable = true
  }
  if (ele.saveConfig.peaceCopyable === undefined) {
    ele.saveConfig.peaceCopyable = ele.peaceCopyable
  }*/

  // 设置默认的必填状态
  if (ObjectUtil.isNullOrUndefined(ele.saveConfig.required)) {
    ele.saveConfig.required = true
  }

  // 如果未设置key，使用列的key
  /*if (!ele.saveConfig.key) {
    ele.saveConfig.key = ele.key as string
  }*/

  // 如果未设置optionConfig，使用外层的optionConfig
 /* if (!ele.saveConfig.optionConfig && ele.optionConfig) {
    ele.saveConfig.optionConfig = ele.optionConfig
  }*/

  // 如果未设置渲染函数，使用外层的渲染函数
 /* if (!ele.saveConfig.peaceRender && ele.peaceRender) {
    ele.saveConfig.peaceRender = ele.peaceRender
  }*/
}
/**
 * 初始化详情配置
 * @param ele 列配置项
 */
const initColumnDetailConfig = (ele: PeaceColumn) => {

  initAbleConfigAndSoOn(ele,'detailConfig','peaceDetailable')
  /*// 如果未设置详情显示属性，默认为true
  if (ele.peaceDetailable === undefined) {
    ele.peaceDetailable = true
  }

  // 初始化详情配置
  if (!ele.detailConfig)
  {
    ele.detailConfig = {}
  }

  // 如果是需要选项的组件类型，确保有 optionConfig
  if (ele.componentEnum) {
    const ifOptionComponent = [
      ComponentEnum.SELECT,
      ComponentEnum.TREE_SELECT,
      ComponentEnum.CASCADER,
      ComponentEnum.RADIO,
    ].includes(ele.componentEnum)

    if (ifOptionComponent) {
      // 如果详情配置中没有设置 key，使用列的 key
      if (!ele.detailConfig.key) {
        ele.detailConfig.key = ele.key as string
      }

      // 如果详情配置中没有设置渲染函数，使用列的渲染函数
      if (!ele.detailConfig.peaceRender && ele.peaceRender) {
        ele.detailConfig.peaceRender = ele.peaceRender
      }
    }
  }*/
}
/**
 * 初始化列筛选配置
 * @param ele 列配置项
 */
const initColumnFilterConfig = (ele: PeaceColumn) => {
  initAbleConfigAndSoOn(ele,'filterConfig','peaceFilterable')
  if (ele.filterConfig?.componentEnum === ComponentEnum.DATE && ObjectUtil.isNullOrUndefined(ele.filterConfig.dateType)) {
    ele.filterConfig.dateType = DateTypeEnum.DATE
  }
  // key 已初始化
  // if (ele.filterConfig.repeatRender){
  //   ele.filterConfig.repeatRender.initMarkFileNames(ele.filterConfig.key)
  // }

  /*ele.peaceFilterable = ObjectUtil.isNullOrUndefined(ele.peaceFilterable)
  // 初始化 filterConfig
  const ifEmptyFilterConfig = ObjectUtil.isEmpty(ele.filterConfig)
  if (ifEmptyFilterConfig) {
    ele.filterConfig = {}
  }
  if (!ifEmptyFilterConfig) {
    ele.filterConfig.autoExtend = ObjectUtil.isNullOrUndefined(ele.filterConfig.autoExtend)
      ? false
      : ele.filterConfig.autoExtend
  } else {
    ele.filterConfig.autoExtend = true
  }
  if (ObjectUtil.isNullOrUndefined(ele.filterConfig.filterable) && ele.filterConfig.autoExtend) {
    ele.filterConfig.filterable = ele.peaceFilterable
  }
  if (ObjectUtil.isNullOrUndefined(ele.filterConfig.key) && ele.filterConfig.autoExtend) {
    ele.filterConfig.key = ele?.key
  }
  if (ObjectUtil.isNullOrUndefined(ele.filterConfig.optionConfig) && ele.filterConfig.autoExtend) {
    ele.filterConfig.optionConfig = ele?.optionConfig
  }
  if (ObjectUtil.isNullOrUndefined(ele.filterConfig.componentEnum) && ele.filterConfig.autoExtend) {
    ele.filterConfig.componentEnum = ele?.componentEnum
  }*/
}
/**
 * 初始化列下拉选项值
 * @param ele 列配置项
 */
const initColumnOption = (ele: PeaceColumn) => {
  if (ObjectUtil.isNullOrUndefined(ele?.componentEnum)) {
    return
  }
  const ifOptionComponent = [
    ComponentEnum.SELECT,
    ComponentEnum.TREE_SELECT,
    ComponentEnum.CASCADER,
    ComponentEnum.RADIO,
  ].includes(ele.componentEnum)
  if (!ifOptionComponent) {
    return
  }
  if (ele.optionConfig) {
    ele.optionConfig = initColumnPeaceOption(ele.optionConfig)
  }
  if (BooleanUtil.isTrue(ele.filterConfig.autoExtend)) {
    if (ObjectUtil.isNullOrUndefined(ele.filterConfig.optionConfig)) {
      ele.filterConfig.optionConfig = ele.optionConfig
    } else {
      ele.filterConfig.optionConfig = initColumnPeaceOption(ele.filterConfig.optionConfig)
    }
  }
}
/**
 * 初始化列操作按钮
 * @param columnz 列配置项
 */
const initColumnPeaceOperateBtn = (columnz: PeaceColumn,tableAction:(rowData?: object, rowIndex?: number, item: PeaceActionBtn) => void ) => {
  if (ArrayUtil.isEmpty(columnz.rowBtnList)) {
    return
  }
  const renderArr: ((rowData: object, rowIndex: number) => any)[] = []
  columnz.rowBtnList.forEach((item) => {
      renderArr.push((rowData: object, rowIndex: number) => {
        if (item?.peaceRender) {
          return item.peaceRender(rowData)
        }
        return h(
          NButton,
          {
            size: item.size ? item.size : 'small',
            type: item.type ? item.type : 'default',
            onClick: () => tableAction(rowData, rowIndex, item),
          },
          { default: () => item.name },
        )
      })
  })
  columnz.render = (rowData: object, rowIndex: number) => {
    const buttonNodes = renderArr.map((renderFn) => renderFn(rowData, rowIndex))
    return h(
      NFlex,
      {
        gap: '2px',
      },
      { default: () => buttonNodes },
    )
  }
}
